import 'package:flutter/material.dart';
import 'package:flutter_demo/controllers/home_controller.dart';
import 'package:flutter_demo/pages/article_list_page.dart';
import 'package:flutter_demo/pages/article_search_page.dart';
import 'package:flutter_demo/pages/category_page.dart';
import 'package:flutter_demo/pages/user_center_page.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

import '../utils/const.dart';

class HomePage extends GetView<HomeController> {
  const HomePage({Key? key}) : super(key: key);

  final List<String> tabs = const ["首页", "分类", "我的"];

  final List<BottomNavigationBarItem> bottomNavItems = const [
    BottomNavigationBarItem(
      backgroundColor: Colors.blue,
      icon: Icon(Icons.home),
      label: "首页",
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.green,
      icon: Icon(Icons.dashboard),
      label: "分类",
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.red,
      icon: Icon(Icons.person),
      label: "我的",
    ),
  ];

  final List<Widget> pages = const [
    ArticleListPage(),
    CategoryPage(),
    UserCenterPage(),
  ];

  @override
  StatelessElement createElement() {
    Get.put(HomeController());
    return super.createElement();
  }

  @override
  Widget build(BuildContext context) {
    var time = 0;
    return Obx(
      () => WillPopScope(
        child: Scaffold(
          appBar: AppBar(
            titleSpacing: 0,
            leading: Text(
              tabs[controller.tabIndex.value],
              style: const TextStyle(fontSize: 18, height: 2, fontWeight: FontWeight.bold),
              textAlign: TextAlign.center,
            ),
            title: controller.tabIndex.value == 2
                ? null
                : GestureDetector(
                    onTap: () => Get.to(() => const ArticleSearchPage()),
                    child: Container(
                      height: 34,
                      alignment: Alignment.center,
                      width: Size.infinite.width,
                      child: const Text(
                        "塞尔达/龙珠Z",
                        style: TextStyle(color: Colors.grey, fontSize: 14),
                      ),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(20),
                      ),
                    ).paddingOnly(right: 13),
                  ),
            elevation: 0,
            toolbarHeight: Const.kAppBarHeight,
          ),
          body: pages[controller.tabIndex.value],
          bottomNavigationBar: BottomNavigationBar(
            items: bottomNavItems,
            currentIndex: controller.tabIndex.value,
            onTap: (index) {
              controller.setTabIndex(index);
            },
          ),
        ),
        onWillPop: () async {
          time++;
          Future.delayed(const Duration(seconds: 3), () => time = 0);
          if (time == 2) {
            return true;
          }
          Fluttertoast.showToast(msg: "再按一次退出");
          return false;
        },
      ),
    );
  }
}
